package org.jeecg.modules.solitaire_app.app.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.solitaire_app.app.entity.VO.OilDatasVO;
import org.jeecg.modules.solitaire_app.app.entity.YsyOilRawMaterialPrice;
import org.jeecg.modules.solitaire_app.app.mapper.YsyOilRawMaterialMapper;
import org.jeecg.modules.solitaire_app.app.mapper.YsyOilRawMaterialPriceMapper;
import org.jeecg.modules.solitaire_app.app.service.IYsyOilRawMaterialPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

/**
 * @Description: 油原料
 * @Author: jeecg-boot
 * @Date: 2023-10-16
 * @Version: V1.0
 */
@Service
@DS("ysy_db")
public class YsyOilRawMaterialPriceServiceImpl extends ServiceImpl<YsyOilRawMaterialPriceMapper, YsyOilRawMaterialPrice> implements IYsyOilRawMaterialPriceService {

    @Autowired
    private YsyOilRawMaterialMapper ysyOilRawMaterialMapper;


    public static void main(String[] args) {
        try {
            // 下载原始图片
            URL originalImageUrl = new URL("https://nwhtupian.zcaiai.com/@/inline/2023-10-26/eb63dc28f26243da8430e95517b5f08a.png");
            BufferedImage originalImage = ImageIO.read(originalImageUrl);

            // 创建一个Graphics2D对象，用于在图像上绘制水印
            Graphics2D graphics = originalImage.createGraphics();

            // 添加标题水印
            String watermarkText = "素材大全海外素材大全";
            Font font = new Font("宋体", Font.BOLD, 50); // 使用宋体字体
            Color color = Color.WHITE;
            graphics.setFont(font);
            graphics.setColor(color);
            graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            graphics.drawString(watermarkText, 430, 150);

            // 添加成员信息水印
            String watermarkText1 = "3334.9K";
            Font font1 = new Font("宋体", Font.BOLD, 50); // 使用宋体字体
            Color color1 = Color.WHITE;
            graphics.setFont(font1);
            graphics.setColor(color1);
            graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            graphics.drawString(watermarkText1, 430, 300);

            // 添加成员信息水印
            String watermarkText2 = "成员";
            Font font2 = new Font("宋体", Font.BOLD, 46); // 使用宋体字体
            Color color2 = Color.WHITE;
            graphics.setFont(font2);
            graphics.setColor(color2);
            graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            graphics.drawString(watermarkText2, watermarkText1.length() == 7 ? 640
                    : watermarkText1.length() == 6 ? 620
                    : watermarkText1.length() == 5 ? 600
                    : watermarkText1.length() == 4 ? 580
                    : watermarkText1.length() == 3 ? 560
                    : 540, 300);

            // 下载水印图片
            URL watermarkImageUrl = new URL("https://nwhtupian.zcaiai.com/@/inline/2023-10-26/b4aaebfc443346fb9d4ba3817610d86b.jpg");
            BufferedImage watermarkImage = ImageIO.read(watermarkImageUrl);

            //缩放水印图片
            watermarkImage = resizeImage(watermarkImage, 320, 320);

            // 添加图片水印
            graphics.drawImage(watermarkImage, 60, 60, null);

            // 保存带有水印的图片到字节数组
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ImageIO.write(originalImage, "jpg", outputStream);
            // 将字节数组转为Base64编码
            String base64Image = Base64.getEncoder().encodeToString(outputStream.toByteArray());

            // 打印Base64编码
            System.out.println(base64Image);

            // 保存带有水印的图片
            File outputImageFile = new File("F:\\opt\\test.jpg");
            ImageIO.write(originalImage, "jpg", outputImageFile);

            graphics.dispose();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 缩放图像到指定的宽度和高度
    private static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
        BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, originalImage.getType());
        Graphics2D graphics = resizedImage.createGraphics();
        graphics.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null);
        graphics.dispose();
        return resizedImage;
    }


    @Override
    public Result<OilDatasVO> getOilNewData(Map<String, Object> map) {
        Result<OilDatasVO> result = new Result<>();
        try {
            //source==1,挂牌价，2-优惠价
            OilDatasVO oilDatasVOS = ysyOilRawMaterialMapper.getOilNewData(map);

            if (ObjectUtil.isEmpty(oilDatasVOS)) {
                OilDatasVO oilDatasVO = new OilDatasVO();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
                oilDatasVO.setPrice(new BigDecimal(0));
                oilDatasVO.setDates(simpleDateFormat.format(new Date()));
                oilDatasVOS = oilDatasVO;
            }

            result.setCode(200);
            result.setResult(oilDatasVOS);
            result.setSuccess(true);
            return result;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setCode(500);
            result.setMessage("服务器响应失败");
            result.setSuccess(false);
            return result;
        }
    }

    @Override
    public Result<List<OilDatasVO>> getOilDatasVOs(Map<String, Object> map) {
        Result<List<OilDatasVO>> result = new Result<>();
        try {
            //source==1,挂牌价，2-优惠价
            List<OilDatasVO> oilDatasVOS = ysyOilRawMaterialMapper.getOilDatasVOs(map);

            Collections.reverse(oilDatasVOS);

            int type = Integer.parseInt(map.get("type").toString());
            if (type == 1) {
                oilDatasVOS.stream().forEach(item -> {
                    item.setDates(item.getDates().split(":")[0] + ":" + item.getDates().split(":")[1]);
                });
                result.setResult(oilDatasVOS);
            }

            if (type == 3){
                SimpleDateFormat sdf = new SimpleDateFormat("MM");
                String dates = sdf.format(new Date());
                List<OilDatasVO> oilDatasVOSNew = new ArrayList<>();
                oilDatasVOS.stream().forEach(item->{
                    if (item.getDates().equals(dates+"01") ||
                            item.getDates().equals(dates+"05") ||
                            item.getDates().equals(dates+"10") ||
                            item.getDates().equals(dates+"15") ||
                            item.getDates().equals(dates+"20") ||
                            item.getDates().equals(dates+"25")){
                        oilDatasVOSNew.add(item);
                    }
                });
                result.setResult(oilDatasVOSNew);
            }

            if (type == 2){
                result.setResult(oilDatasVOS);
            }

            result.setCode(200);
            result.setSuccess(true);
            return result;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setCode(500);
            result.setMessage("服务器响应失败");
            result.setSuccess(false);
            return result;
        }
    }

    @Override
    public YsyOilRawMaterialPrice getOneOilPrice(int oriId, int refineryId) {
        return ysyOilRawMaterialMapper.getOneOilPrice(oriId, refineryId);
    }

    @Override
    public YsyOilRawMaterialPrice getOneOilPriceByRefId(int refineryId) {
        return ysyOilRawMaterialMapper.getOneOilPriceByRefId(refineryId);
    }
}
